<template>
  <div ref="dom" class="charts chart-line"></div>
</template>

<script>
import echarts from 'echarts'
import tdTheme from './theme.json'
import { on, off } from '@/libs/tools'
echarts.registerTheme('tdTheme', tdTheme)
export default {
  name: 'Shebao',

  props: {
    value: Array,
    text: String
  },

  data () {
    return {
      dom: null
    }
  },

  watch: {
    value: {
      // immediate: true,
      handler (val, oldVal) {
        this.value = val
        this.initLine()
      }
    }
  },

  mounted () {
    this.$nextTick(() => {
      this.initLine()
    })
  },

  methods: {
    resize () {
      this.dom.resize()
    },

    initLine () {
      let xAxisData = this.value.map(item => {
        // return item.periodYear + '-' + item.periodMonth
        return item.periodMonth + '月'
      })
      let seriesData = this.value.map(item => {
        return item.total
      })
      let option = {
        title: {
          text: this.text,
          x: 'left',
          textStyle: {
            color: '#7391c4',
            fontSize: '16',
            fontWeight: 'normal'
          }
        },
        grid: { top: 40, bottom: 20, left: 60, right: 20 },
        tooltip: {
          trigger: 'item',
          formatter: '{b} :  {c}元',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        xAxis: {
          type: 'category',
          // boundaryGap : false,
          data: xAxisData,
          position: 'bottom',
          splitLine: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: '#999'
            }
          },
          axisLine: {
            lineStyle: {
              color: '#999'
            }
          }
        },
        yAxis: {
          splitNumber: 4,
          axisLabel: {
            textStyle: {
              color: '#999'
            }
          },
          axisLine: {
            lineStyle: {
              color: '#fff'
            }
          },
          splitLine: {
            lineStyle: {
              color: '#e5e5e5'
            }
          }
        },
        series: [
          {
            type: 'line',
            symbol: 'circle',
            smooth: true,
            symbolSize: 3,
            lineStyle: {
              normal: {
                color: '#9292f8'
              }
            },
            itemStyle: {
              normal: {
                label: { show: true },
                color: '#9292f8'
              }
            },
            areaStyle: {
              color: '#e2e4fd'
            },
            data: seriesData
          }
        ]
      }
      this.dom = echarts.init(this.$refs.dom, 'tdTheme')
      this.dom.setOption(option)
      on(window, 'resize', this.resize)
    }
  },
  beforeDestroy () {
    off(window, 'resize', this.resize)
  }
}
</script>
